package homework_4_minesweeper;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MineSweeperMain extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private GameBoardPanel mines=new GameBoardPanel();
    private NorthMenu menu=new NorthMenu(this);
    private SouthMenu info=new SouthMenu();
    private static boolean clickable=true;       //设计这个的目的是，在判定输、赢以及某些情况时是不能点击方块的，要让全局知道这个变量的状态
    private Container mainScreen;
    public MineSweeperMain(){
        mainScreen=this.getContentPane();
        mainScreen.setLayout(new BorderLayout());
        mainScreen.add(mines,BorderLayout.CENTER);
        mainScreen.add(menu,BorderLayout.NORTH);
        mainScreen.add(info,BorderLayout.SOUTH);
        mines.newGame(9,9,10);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Minesweeper");
        try {
            Image icon= ImageIO.read(MineSweeperMain.class.getResource("images/game_icon.png"));
            setIconImage(icon);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("图标加载失败，使用默认图标");
        }
        setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new homework_4_minesweeper.MineSweeperMain();
            }
        });
    }

    public static boolean isClickable() {
        return clickable;
    }

    public static void setClickable(boolean clickable) {
        MineSweeperMain.clickable = clickable;
    }
    public GameBoardPanel getPanel(){
        return mines;
    }

    @Override
    public void actionPerformed(ActionEvent e) {            //上方菜单“NorthMenu”的监听器
        if(e.getSource()== NorthMenu.getExitChoice()){
            System.exit(0);
        }
        if(e.getSource()== NorthMenu.getResetThis()){
            mines.reset();
        }
        if (e.getSource()== NorthMenu.getResetAll()){
            MineMap.clearMap();
            mines.newGame();
        }
        if (e.getSource()== NorthMenu.getNewGameEasy()){
            MineMap.clearMap();
            mines.newGame(9,9,10);
        }
        if(e.getSource()== NorthMenu.getNewGameIntermediate()){
            MineMap.clearMap();
            mines.newGame(16,16,40);
        }
        if(e.getSource()== NorthMenu.getNewGameDifficult()){
            MineMap.clearMap();
            mines.newGame(16,30,99);
        }
        pack();
        revalidate();
        repaint();
    }
}
